// pages/ordinaryDetails/ordinaryDetails.js
const app = getApp()
const utils = require('../../utils/util.js')
const CryptoJS = require('../../utils/aes-jia.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    id: '',
    redId: '',
    shareshow: false,
    list: [],
    totalPages: '',
    pageNumber: 1,
    inputname: '',
    userInfo: '',
    openShow: false,
    returnShow: false,
    gobackShow: false,
    receiveShow: false,
    groupShow: false,
    myred: false,
    myshow: '',
    group: '',
    groupList: {},
    isOpen: false,
    sendUrl: '',
    sendName: '',
    redBlessings: '',
    receiveName: '',
    receiverUrl: '',
    type: 1,
    redPacket: '',
    amountRedEnvelope: '',
    amountRedEnvelopeico: '',
    givingAdvice: '',
    walletBalance: '',
    payFocus: false,
    showPayPwdInput: false,
    pwdVal: '',
    isOver: true,
    senderId: '',
    returninput: '',
    phoneshow: false,
    loginShow: false,
    login: false,
    phone: false,
    baseData: [],
    givingAdvice: '',
    dirty: [],
  },
  login() {
    if (this.data.login) {
      this.toggle('phoneshow', true)
      this.selectComponent('#phone').showClose()
    } else {
      this.toggle('loginShow', true)
      this.selectComponent('#login').showClose()
    }
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.onShow()
  },
  toggleToast1(e) {
    console.log(e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
    this.onShow()
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  tohome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //点击送祝福
  topLeaveMsg: function () {
    this.setData({
      focus: true
    })
  },
  emit() {
    this.setData({
      pageNumber: 1,
      list: []
    })
    // console.log('领取完',this.data.pageNumber)
    this.getlist()
  },
  //送祝福
  formName: function (e) {
    this.setData({
      inputname: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindblur: function (e) {
    this.setData({
      // inputname: e.detail.value.replace(/\s+/g, ''),
      focus: false
    })
  },
  //确认  
  confirmreply: function (e) {
    let that = this
    console.log('发送', that.data.inputname)
    if (that.data.inputname == '') {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (that.data.inputname != '') {
        var n = 0
        // console.log('词库2', this.data.dirty.length)
        for (var i = 0; i < this.data.dirty.length; i++) {
          // console.log('循环')
          if (that.data.inputname.indexOf(this.data.dirty[i]) >= 0) {
            console.log('查询2')
            wx.showToast({
              title: '您输入的内容有违禁词，敏感词已被替换成星号,请编辑后重新提交',
              icon: 'none',
              duration: 2000
            })
            var fw = this.data.dirty[i]
            var fw1 = ''
            for (var k = 0; k < fw.length; k++) {
              fw1 = fw1 + '*'
            }
            var activityProcess = that.data.inputname.replace(fw, fw1)
            that.setData({
              inputname: activityProcess
            });
            console.log('替换', that.data.inputname)
            // console.log('替换1', activityProcess)
            n = n + 1
          }
        }
        if (n > 0) {
          return
        }
      }
      app.req({
        url: "blessmini/blessword/save",
        method: "post",
        data: {
          blessActId: that.data.id,
          blessActType: 1,
          content: that.data.inputname,
          receiveId: that.data.item.userId
        },
        success: function (res) {
          console.log(res)
          if (res.code == 200) {
            wx.showToast({
              title: '祝福成功',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              inputname: ''
            })
            that.onShow()
          } else {
            wx.showToast({
              title: '发送失败',
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    }
    this.setData({
      focus: false
    })
  },
  topage() {
    var my = 0
    if (this.data.item.userId == this.data.userInfo.id) {
      my = 1
    }
    wx.navigateTo({
      url: '/pages/outRed/outRed?actId=' + this.data.item.id + "&receiveId=" + this.data.item.userId + "&actType=" + 1 + "&name=" + this.data.item.name + '&my=' + my,
    })
  },
  getdetails() {
    var that = this
    app.req({
      url: "blessmini/blesscommon/birthdayDetails",
      data: {
        id: that.data.id,
        actType: 1
      },
      method: "post",
      success: function (res) {
        console.log("item中数据", res)
        if (res.data.birthday) {
          res.data.birthday = utils.itemDateToShow(res.data.birthday, 1, false, false).substring(5, 11)
        }
        that.setData({
          item: res.data,
          baseData: res.data.blessingList
        })
        that.setDM()
        console.log('活动详情', that.data.item)
      }
    })
  },
  getlist() {
    var that = this
    app.req({
      url: "blessmini/blesscomment/blessingList",
      data: {
        actId: that.data.id,
        pageNumber: that.data.pageNumber,
        pageSize: 10
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          for (var item of res.data.page.rows) {
            item.createTime = item.createTime.substring(5, 16)
          }
          that.setData({
            list: that.data.list.concat(res.data.page.rows),
            totalPages: res.data.page.totalPages
          })
          console.log('活动列表', that.data.list)
        }
      }
    })
  },

  //获取钱包余额
  getWalletBalance: function () {
    let that = this
    app.req({
      url: "blessmini/user/myBalance",
      method: "post",
      data: '',
      success: function (res) {
        console.log('获取钱包余额', res)
        if (200 == res.code) {
          that.setData({
            walletBalance: res.data.balance
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //获取是否有密码
  getIspswd() {
    var that = this
    app.req({
      url: 'blessmini/user/whetherSetPayPwd',
      data: {
      },
      method: 'POST',
      success: function (res) {
        console.log('密码', res);
        if (res.code == 200) {
          that.setData({
            whetherSetPayPwd: res.data.whetherSetPayPwd
          })
        }
      }
    })
  },
  setDM: function () {
    // 处理弹幕参数
    const dmArr = [];
    const _b = this.data.baseData;
    for (let i = 0; i < _b.length; i++) {
      const time = Math.floor(Math.random() * 10);
      const _time = time < 6 ? 6 + i : time + i;
      const top = Math.floor(Math.random() * 80) + 2;
      const _p = {
        sendAvatarUrl: _b[i].sendAvatarUrl,
        content: _b[i].content,
        top,
        time: _time,
      };
      dmArr.push(_p);
    }
    this.setData({
      dmData: dmArr
    });
  },
  // 祝福全读
  getallRead() {
    var that = this
    app.req({
      url: 'blessmini/blesscomment/allRead',
      data: {
        actType: 1,
        actId: that.data.id
      },
      method: 'POST',
      success: function (res) {
        if (res.data == 200) {
          console.log('全读');
        }
      }
    })
  },
  getdirty() {
    var that = this
    app.req({
      url: "/wechat/dirty/all",
      method: "post",
      success: function (res) {
        // console.log('脏字库')
        if (res.code == 200) {
          wx.setStorageSync('dirty', res.data);
          that.setData({
            dirty: res.data
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("寿星ID", options)
    var that = this
    that.setData({
      id: options.id
    })
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo
      })
    })
    if (wx.getStorageSync('dirty') == undefined || wx.getStorageSync('dirty') == '') {
      that.getdirty()
    } else {
      that.setData({
        dirty: wx.getStorageSync('dirty')
      });
    }
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      shareshow: false,
      pageNumber: 1,
      list: [],
    })
    // console.log('刷新',that.data.pageNumber)
    if (wx.getStorageSync('uid') == '') {
      that.toggle("login", false);
    } else {
      that.toggle("login", true);
      var phone = wx.getStorageSync("phone")
      if (phone == '') {
        that.toggle("phone", true);
      } else {
        that.toggle("phone", false);
        that.toggle("phoneshow", false);
        that.toggle("loginShow", false);
        app.getUserInfo(function (userInfo) {
          that.toggle("userInfo", userInfo);
          that.getWalletBalance()
          that.getIspswd()
        })
      }
    }
    that.getdetails()
    that.getallRead()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    this.getlist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    // console.log("走刷新")
    if (that.data.totalPages > that.data.pageNumber) {
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })
      that.getlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    that.setData({
      shareshow: true,
    })
    return {
      title: '祝' + that.data.item.name + '生日快乐',
    }
  }
})